import pygame, unittest

class Map():
    def __init__(self):
        self.hasCanvas = False
        self.hasPath = False
        self.hasSpawn = False
        self.hasHome = False

    def setCanvas(self, image):
        self.canvas = image
        self.hasCanvas = True

    def getWidth(self):
        return self.canvas.get_width()

    def getHeight(self):
        return self.canvas.get_height()

    def size(self):
        return self.getWidth(),self.getHeight()

    def isReady(self):
        return self.hasCanvas and self.hasPath and self.hasSpawn and self.hasHome

    def detectPath(self, color):
        self.pathColor = color
        self.path = []
        for x in range(self.canvas.get_width()):
            for y in range(self.canvas.get_height()):
                if self.canvas.get_at((x,y)) == color:
                    self.path.append((x,y))
        self.hasPath = len(self.path) > 0       
        return self.hasPath

    def detectSpawn(self, color):
        self.spawnColor = color
        self.hasSpawn = True
        self.spawn = []
        for x in range(self.canvas.get_width()):
            for y in range(self.canvas.get_height()):
                if self.canvas.get_at((x,y)) == color:
                    self.spawn.append((x,y))
        self.hasSpawn = len(self.spawn) > 0
        return self.hasSpawn

    def detectHome(self, color):
	self.homeColor = color
	self.home = []
        for x in range(self.canvas.get_width()):
            for y in range(self.canvas.get_height()):
                if self.canvas.get_at((x,y)) == color:
                    self.home.append((x,y))
        self.hasHome = len(self.home) > 0
        return self.hasHome

    def correctGradient(self, grad, speed, pos):
	x = pos[0];
	y = pos[1];
	speedCircle = x*x + y*y - speed*speed
	newGrad = (y-pos[1])/(x-pos[0])
	return newGrad;


class MapTestCase(unittest.TestCase):
    def setUp(self):
        map = Map()
        map.setCanvas(pygame.image.load('../res/snake.png'))
        map.detectPath(pygame.Color('0x000000'))
        map.detectSpawn(pygame.Color('0xde1919'))
        map.detectHome(pygame.Color('0x3019de'))
	self.map = map

    def testCreateMap(self):
        self.assertEquals(600, self.map.getWidth())
        self.assertEquals(400, self.map.getHeight())
        self.assertEquals((600,400),self.map.size())
        self.assertTrue(self.map.isReady())

    def testAdviseEnemy(self):
        self.assertEquals(0, self.map.correctGradient(0,1,(64,40)))
        self.assertEquals(270, self.map.correctGradient(0,1,(252,77)))

if __name__ == '__main__':
    unittest.main()
